//
//  YTNewFeatureView.m
//  yitaicaijing
//
//  Created by DIANGHONG on 2018/8/7.
//  Copyright © 2018年 DIANGHONG. All rights reserved.
//

#import "YTNewFeatureView.h"
@interface YTNewFeatureView()<UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UIButton *openButton;
@property(nonatomic ,copy)void(^finishBlock)(void);

@end
@implementation YTNewFeatureView

#pragma mark : 类方法 实例化新特性界面
+ (instancetype)newFeaturViewWithFinishedBlock:(void(^)(void))finishedBlock
{
    
    UINib *nib = [UINib nibWithNibName:@"YTNewFeatureView" bundle:nil];
    
    YTNewFeatureView *view = [nib instantiateWithOwner:nil options:0][0];
    
    view.frame = [UIScreen.mainScreen bounds];
    view.finishBlock = finishedBlock;
    
    return (YTNewFeatureView *)view;
}

#pragma mark: - 开启按钮 监听
- (IBAction)openButtonClick:(UIButton *)sender
{
  
    [NSNotificationCenter.defaultCenter postNotificationName:@"kShowFirstDLNotification" object:nil];
    self.finishBlock();
    [self removeFromSuperview];
}
- (void)awakeFromNib{
    [super awakeFromNib];
    
    self.openButton.alpha = 0;
    //图片数量
    int imgCount = 3;
    
    //设备bounds
    CGRect rect = [UIScreen.mainScreen bounds];
    
    for (int i = 0; i < imgCount; i++) {
        
        //设置image
        NSString *imgName = [NSString stringWithFormat:@"ydy_%d",i + 1];
        
        //创建UIImageView并设置图像
        UIImageView *imgView = [[UIImageView alloc] initWithImage: [UIImage imageNamed:imgName]];
        
        imgView.frame = CGRectOffset(rect, rect.size.width * i, 0);
        
        [self.scrollView addSubview:imgView];
    }
    
    //设置scrollView
    self.scrollView.contentSize = CGSizeMake((imgCount) * rect.size.width, rect.size.height);
    self.scrollView.showsVerticalScrollIndicator = false;
    self.scrollView.showsHorizontalScrollIndicator = false;
    self.scrollView.pagingEnabled = true;
    self.scrollView.bounces = false;
    
    //设置scrollView代理
    self.scrollView.delegate = self;
    
    //设置按钮监听
//    [self.enterButton addTarget:self action:@selector(enterButtonClick) forControlEvents:UIControlEventTouchUpInside];
    
    //隐藏按钮
    //self.enterButton.hidden = true;
}

// MARK: - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    //取出当前页数
    int page = self.scrollView.contentOffset.x / scrollView.bounds.size.width;
    
    //判断是否是最后一页
    if (page == scrollView.subviews.count - 1) {
        
        //移除该视图
        self.openButton.enabled = YES;
        
        
        self.openButton.alpha = 1;
       
        
    }else{
        
     
        self.openButton.alpha = 0;
        
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{

    self.openButton.alpha = 0;
}
@end
